﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Net;
using System.Runtime.Serialization.Json;
using System.IO;
using Redboil.Bfp.Model;
using Redboil.Bfp.Core;

namespace Redboil.Bfp.Client {
    public partial class SampleWindow : Window {
        

        public SampleWindow() {
            InitializeComponent();
        }

        private void buttonCreate_Click(object sender, RoutedEventArgs e) {
            var client = new WebClient();
            //client.BaseAddress = "http://localhost:1000";
            client.Headers[HttpRequestHeader.ContentType] = "text/json";
            client.Encoding = Encoding.UTF8;

            IList<User> userList = new List<User>() {
                new User() { Account = "Gene", IsAdministrator = true, Password = "123456" }
            };
            var json = JsonHelper.Serialize<User>(userList);

            client.UploadStringCompleted += (o, ea) => {
                var x = ea.Result;
            };
            client.UploadStringAsync(new Uri("http://localhost:1000/SampleService"), "POST", json);

            //var serializer = new DataContractJsonSerializer(typeof(User));
            //using(var stream = new MemoryStream()) {
            //    serializer.WriteObject(stream, user);
            //    var json = Encoding.UTF8.GetString(stream.ToArray());
            //    client.UploadStringAsync(new Uri("http://localhost:1000/SampleService"), "POST", json);
            //}
        }

        private void buttonRead_Click(object sender, RoutedEventArgs e) {
            var client = new WebClient();
            client.DownloadStringCompleted += (o, ea) => {
                //MessageBox.Show(ea.Result);
                var userList = JsonHelper.Deserialize<User>(ea.Result);
                MessageBox.Show(userList.ToString());
            };
            client.DownloadStringAsync(new Uri("http://localhost:1000/SampleService"));
        }

        private void buttonUpdate_Click(object sender, RoutedEventArgs e) {

        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e) {

        }
    }
}
